import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        component: () => import('../views/login.vue')
    },
    {
        path: '/registry',
        component: () => import('../views/registry.vue')
    },
    {
        path: '/main',
        component: () => import('../views/main/index.vue'),
        children:[
            {
                path:'/main/home',
                component:() => import('../views/main/home/index.vue'),
                children:[
                    {
                        path:'/main/home/word',
                        component:() => import('../views/main/home/word.vue'),
                    },
                    {
                        path:'/main/home/store',
                        component:() => import('../views/main/home/store.vue'),
                    },
                    {
                        path:'/main/home/collect',
                        component:() => import('../views/main/home/collect.vue'),
                    },
                    {
                        path:'/main/home/follow',
                        component:() => import('../views/main/home/follow.vue'),
                    },
                    {
                        path:'/main/home/person',
                        component:() => import('../views/main/home/person.vue'),
                    },
                    {
                        path:'/main/home',
                        redirect:'/main/home/word'
                    }
                ]
            },
            {
                path:'/main/detail',
                component:() => import('../views/main/detail.vue')
            },
            {
                path:'/main/addStore',
                component:() => import('../views/main/addStore.vue')
            },
            {
                path:'/main/addWord',
                component:() => import('../views/main/addWord.vue')
            },
            {
                path:'/main/search',
                component:() => import('../views/main/search.vue')
            },
            {
                path:'/main',
                redirect:'/main/home'
            }
        ]
    },
    {
        path:'/',
        redirect:'/main'
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  linkActiveClass:'active'
})

//全局路由守卫
router.beforeEach((to,from,next) => {
    let writePath = ['/login','/registry'];
    if(writePath.includes(to.path)){
        next()
    }else{
        if(localStorage.getItem('token')){
            next()
        }else{
            next('/login')
        }
    }
})

export default router
